﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using ApocDev.TownsApi;

namespace TownsModder
{
    /// <summary>
    /// Interaction logic for LoadingWindow.xaml
    /// </summary>
    public partial class LoadingWindow : Window
    {
        private StatusWrapper _status;

        public class StatusWrapper : INotifyPropertyChanged
        {
            private string _fileName;

            public string FileName
            {
                get { return _fileName; }
                set
                {
                    _fileName = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("FileName"));
                }
            }

            private int _itemsCount = 50;

            public int ItemsCount
            {
                get { return _itemsCount; }
                set
                {
                    _itemsCount = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("ItemsCount"));
                }
            }

            private int _maxitemsCount = 100;

            public int MaxItemsCount
            {
                get { return _maxitemsCount; }
                set
                {
                    _maxitemsCount = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("MaxItemsCount"));
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
        }
        public LoadingWindow()
        {
            InitializeComponent();
            DataContext = (_status = new StatusWrapper());
            Loaded += (s, e) => ThreadPool.QueueUserWorkItem(o =>
            {
                ModFiles.FileLoadUpdate += HandleFileLoadUpdate;
                ModFiles.Initialize(@"C:\Users\Jim\Desktop\Towns (Original)\data");
            });

        }

        private void HandleFileLoadUpdate(object sender, FileLoadEventArgs e)
        {
            if (Dispatcher.Thread != Thread.CurrentThread)
                Dispatcher.Invoke(new EventHandler<FileLoadEventArgs>(HandleFileLoadUpdate), sender, e);
            else
            {
                if (e.FileName == "Finished")
                    Close();

                _status.FileName = e.FileName + " " + e.Current + "/" + e.Total;
                _status.ItemsCount = e.Current;
                _status.MaxItemsCount = e.Total;
            }
        }
    }
}
